package com.yf.modules.system.controller;

import com.yf.Utils.PageUtil;
import com.yf.domain.security.Department;
import com.yf.model.query.DeptQueryCriteria;
import com.yf.modules.system.service.api.DepartmentService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * @author chentianwei
 * @date 2024-10-15 10:46
 * @description 部门操作Api
 */
@RestController
@Slf4j
@RequestMapping("/api/dept")
@Tag(name = "DepartmentApi", description = "部门Api")
public class DepartmentController {

    private final DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {

        this.departmentService = departmentService;
    }

    @GetMapping("")
    @Operation(summary = "查询部门明细", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> selectDept(DeptQueryCriteria deptQueryCriteria) {
        List<Department> departments = departmentService.queryDepartment(deptQueryCriteria);
        return ResponseEntity.ok().body(ResponseBody.success(PageUtil.toPage(departments, departments.size())));
    }

    @PostMapping("/superior")
    @Operation(summary = "查询子部门明细", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> getDeptSuperior(@RequestBody List<Long> ids) {
        Collection<?> superior = departmentService.getDeptSuperior(ids);
        Map<String,Object> map = new HashMap<>(2);
        map.put("totalElements",superior.size());
        map.put("content", superior);
        return ResponseEntity.ok().body(ResponseBody.success("操作成功", map));
    }

    @PostMapping("/add")
    @Operation(summary = "添加新部门", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> addDept(@RequestBody Department department) {
        departmentService.addDept(department);
        return ResponseEntity.ok().body(ResponseBody.success("添加成功"));
    }

    @PutMapping("/edit")
    @Operation(summary = "修改部门", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> editDept(@RequestBody Department department) {
        departmentService.editDept(department);
        return ResponseEntity.ok().body(ResponseBody.success("修改成功"));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> deleteDept(@RequestBody Set<Long> ids) {
        departmentService.deleteDept(ids);
        return ResponseEntity.ok().body(ResponseBody.success("删除成功"));
    }

    @GetMapping("/download")
    @Operation(summary = "导出部门数据", security = @SecurityRequirement(name = "authScheme"))
    public void download(HttpServletResponse response) throws IOException {
        departmentService.downloadDept(response);
    }


}
